<?php
///////////////////////////////////////////////////////////////////////////////
//
//  establish database link at load-time
//
///////////////////////////////////////////////////////////////////////////////
if (!array_key_exists('SQL_PRIMARY_RESOURCE', $GLOBALS))
{
	$GLOBALS['SQL_PRIMARY_RESOURCE'] = mysql_connect(constant('DB_DATABASE_HOST'), constant('DB_DATABASE_USER'), constant('DB_DATABASE_PASS'));
	if ($GLOBALS['SQL_PRIMARY_RESOURCE'] == false)
		exit(sprintf("<strong>Error:</strong> %s; Using Server Host: '%s' in: <b>%s</b> at line: <b>%s</b><br>\n", mysql_error(), constant('DB_DATABASE_HOST'), __FILE__, __LINE__));
	mysql_query("SET NAMES 'utf8'");
	mysql_select_db(constant('DB_NAME'));
	echo mysql_error();
}
///////////////////////////////////////////////////////////////////////////////
//
//  simple database query class for mySQL.  Assumes single database link.
//
///////////////////////////////////////////////////////////////////////////////
class DATATABLE_DB_DRIVER
{
	var $resource;
	var $query;
	var $result;
	var $error;

	var $num_rows;
	var $affected_rows;
	var $last_insert_id;

	// constructor runs initial query	
	function DATATABLE_DB_DRIVER($query="")
	{
		$this->num_rows = false;
		$this->affected_rows = false;
		$this->last_insert_id = false;
		$this->result = true;

		if (strlen($query))
			$this->query($query);
	}
	
	// executes query, records error
	function query($query)
	{
		$this->query = $query;
		$this->result = false;
		$this->resource = mysql_query($query, $GLOBALS['SQL_PRIMARY_RESOURCE']);
		if ($this->resource == FALSE)
			$this->error = mysql_error($GLOBALS['SQL_PRIMARY_RESOURCE']);
		else
			$this->result = true;
		return $this->result;
	}
	// executes query, stores rows affected
	function query_affected_rows($query)
	{
		$this->affected_rows = 0;
		if ($this->query($query))
			$this->affected_rows = mysql_affected_rows($GLOBALS['SQL_PRIMARY_RESOURCE']);
		return $this->affected_rows;
	}
	// executes query, stores rows selected
	function query_num_rows($query)
	{
		$this->num_rows = 0;
		if ($this->query($query))
			$this->num_rows = mysql_num_rows($this->resource);
		return $this->num_rows;
	}
	// frees query resources
	function close()
	{
		$this->result = true;
		if ($this->resource != false)
			$this->result = mysql_free_result($this->resource);
		return $this->result;
	}
	
	function delete($query)
	{
		return $this->query_affected_rows($query);
	}
	function insert($query)
	{
		if ($this->query_affected_rows($query))
			$this->last_insert_id = mysql_insert_id($GLOBALS['SQL_PRIMARY_RESOURCE']);
		return $this->result;
	}
	function replace($query)
	{
		return $this->query_affected_rows($query);
	}
	function select($query)
	{
		return $this->query_num_rows($query);
	}
	function update($query)
	{
		return $this->query_affected_rows($query);
	}

	// row fetching functions
	function fetch()
	{
		return $this->fetch_assoc();
	}
	function fetch_assoc()
	{
		return mysql_fetch_assoc($this->resource);
	}
	function fetch_array()
	{
		return mysql_fetch_array($this->resource);
	}
	function fetch_result()
	{
		return mysql_num_rows($this->resource) > 0?mysql_result($this->resource, 0, 0):null;
	}
	
	// stats
	function num_rows()
	{
		return mysql_num_rows($this->resource);
	}
	function affected_rows()
	{
		return mysql_affected_rows($GLOBALS['SQL_PRIMARY_RESOURCE']);
	}
	
	// error output
	function error_exit($file, $line)
	{
		printf("SQL ERROR %s<br>\nquery (%s)<br>\nin: <b>%s</b> at line: <b>%s</b><br>\n", $this->error, $this->query, $file, $line);
	}
}

?>